/**
    content 为传入的内容，可以是html
    transparent 为背景透明度，间隔在1 - 0，越小越透明，建议值：0.8
    time 关闭时间，当大于0时，倒计时有效，单位 秒

    加载中               _ALERT.Loading( 需要关闭的时间描述，不写或为0均为不关闭 );
    关闭                 _ALERT.Close();
    公共提示            _ALERT.Init({content: _ALERT.HTML_Tip('操作成功', 1), url:'/taskShare', time:3, transparent:'0.8'})
        其中 _ALERT.Tip("提示信息", 提示类型[ 1：成功，2：失败])
            url 为地址栏 # 后面的参数

    加金币、签到成功：   _ALERT.Init({content:_ALERT.HTML_Sign('提示信息', '金币数量'), transparent:'0.8'});
    签到失败：           _ALERT.Init({content:_ALERT.HTML_SignFail('回调函数'), transparent:'0.8'});
    申请提现：           _ALERT.Init({content:_ALERT.HTML_TakeNow('回调函数'), transparent:'0.8'});
        注意：申请提现的回调函数请在所在页面写好点击后的js 回调函数；获取输入框的数据采用 $("#tasknow_money").val();
    申请提现成功：       _ALERT.Init({content:_ALERT.HTML_TakeNow_Success('回调函数'), transparent:'0.8'});
    优惠二维码：         _ALERT.Init({content:_ALERT.HTML_Red_Qrcode('二维码图片地址'), transparent:'0.8'});
    恭喜中奖：           _ALERT.Init({content:_ALERT.HTML_Prize('中奖金额', '单位：不写默认为 元 '), transparent:'0.8'});
**/

isTouch = "ontouchend" in document.createElement("div"),
    touch_start = isTouch ? "touchstart" : "mousedown",
    touch_move = isTouch ? "touchmove" : "mousemove",
    touch_end = isTouch ? "touchend" : "mouseup",
    touch_cancel = isTouch ? "touchcancel" : "mouseout";

alert_options = {};
var _ALERT = {
    Init: function (options) {
        $("#ALERT_WIN").remove();
        alert_options = {};
        var DEFAULTS = {
            content: "",
            transparent: 0,
            time: "0",
            url: "",
            callback : null
        };
        alert_options = jQuery.extend({}, DEFAULTS, options || {});
        var window_width = $(window).width();
        var window_height = $(window).height();
        var win = $("#ALERT_WIN");
        if (win.length == 0) {
            $("body").append("<div id='ALERT_WIN'><div id='ALERT_WIN_CONTENT'><div class='ALERT_WIN_CONTENT'></div></div></div>");
            win = $("#ALERT_WIN");
        }
        win.css({
            opacity: 0,
            background: "rgba(0, 0, 0," + alert_options.transparent + ")",
            height: "100%",
            width: "100%",
            position: "fixed",
            top: 0,
            left: 0,
            zIndex: "98"
        });
        var win_content = $("#ALERT_WIN_CONTENT");
        var win_content2 = $(".ALERT_WIN_CONTENT");
        win_content2.html(alert_options.content);
        var winc_width = (win_content2.outerWidth() > window_width ? window_width - 100 : win_content2.outerWidth());
        var winc_height = (win_content2.outerHeight() > window_height ? window_height - 100 : win_content2.outerHeight());
        win_content.css({
            color: "#FFF",
            position: "absolute",
            zIndex: "99",
            width: "100%",
            height: winc_height,
            margin: "0 auto",
            left: (window_width - winc_width) / 2,
            top: (window_height - winc_height) / 2
        });
        win_content2.css({ width: "90%", margin: "0 auto", display: "block" });
        win.animate({ display: "block", opacity: 1 });
        var time = 0;
        time = parseInt(alert_options.time);
        if (time > 0) {
            if (typeof (closeTime) != "undefined") {
                clearInterval(closeTime);
            }
            closeIndex = 0;
            closeTime = setInterval(function () {
                closeIndex++;
                if (closeIndex >= time) {
                    _ALERT.Close();
                }
            }, 1000);
        }
    },
    Tip: function (str, o) {
        $("#TIP_WIN").remove();
        var obj = $(o);
        var win = $("#TIP_WIN");
        if (win.length == 0) {
            $("body").append("<div id='TIP_WIN'></div>");
            win = $("#TIP_WIN");
        }
        win.html(str);
        win.css({
            opacity: 0,
            position: "fixed",
            top: obj.offset().top - 10,
            left: obj.offset().left + (obj.width() / 2) ,
            zIndex: "98",
            color: "#FFF",
            fontSize: "16px"
        });
        win.animate({ display: "block", opacity: 1 });
        var time = 3;
        if (typeof (tipCloseTime) != "undefined") {
            clearInterval(tipCloseTime);
        }
        tipCloseIndex = 0;
        tipCloseTime = setInterval(function () {
            tipCloseIndex++;
            if (tipCloseIndex >= time) {
                $("#TIP_WIN").fadeOut(200, function () { $("#TIP_WIN").remove(); });
            }
        }, 1000);
    },
    Loading: function (_time) {
        _time = parseInt(_time);
        var html = '<div class="alert_loading"><img src="/dj/images/loading.gif" /></div>';
        this.Init({ content: html, transparent: '0.8', time: _time });
    },
    Close: function () {
        $("#ALERT_WIN_CONTENT").fadeOut(200, function () { $("#ALERT_WIN").fadeOut(200, function () { $("#ALERT_WIN").remove(); }) });
        if (typeof (closeTime) != "undefined") {
            clearInterval(closeTime);
        }
        if (alert_options.callback) {
            eval(alert_options.callback);
        }
        url = alert_options.url;
        if (url != "" && typeof (url) != "undefined") {
            window.location.href = window.location.href.split('#')[0] + "#" + url;
        }
    },
    HTML_Tip: function (string, ok) {
        var html = '<div class="alert_tip"><p><h1><i class="fa ' + (ok ? 'fa-check' : 'fa-close') + '"></i></h1></p><p>' + string + '</p></div>';
        return html;
    },
    HTML_Sign: function (string, num) {
        var html = '<div class="alert_sign"><p><h1>' + string + ' !</h1></p><p><span>' + (num > 0 ? '+' + num : num) + '</span>金币</p></div>';
        return html;
    },
    HTML_SignFail: function (callback) {
        var html = '<div class="alert_sign"><p><h1>签到失败 !</h1></p><p><a class="fail StopPropagation" href="javascript:void(0);" onclick="' + callback + '">点击设置允许，方可签到</a></p></div>';
        return html;
    },
    HTML_TakeNow: function (callback) {
        var html = '<div class="alert_tasknow StopPropagation">' +
                    '<p class="_close" onclick="_ALERT.Close();"><i class="fa fa-close"></i></p>' +
                    '<p class="money"><input id="tasknow_money" type="text" value="请输入提现金额" onfocus="if (value ==\'请输入提现金额\'){value =\'\'}" onblur="if (value ==\'\'){value=\'请输入提现金额\'}" /></p>' +
                    '<p class="desc clearfix"><span class="left">注：提取金额最低为<b>100元</b></span><a href="javascript:void(0);" onclick="_ALERT.Close();window.location.href=\'/dj/index#/tixianlishijilu\';" class="right">历史提现记录</a></p>' +

                    '<p class="submit"><a href="javascript:void(0);" onclick="' + callback + '">提交申请</a></p>' +
                    '</div>';
        return html;
    },
    HTML_TakeNow_Success: function (callback) {
        var html = '<div class="alert_tasknow_success"><p><h1><i class="fa fa-check"></i></h1></p><p><h1>提交成功 !</h1></p><p class="s_desc">预计两个工作日内工作人员以红包的方式转发给你</p></div>';
        return html;
    },
    HTML_Red_Qrcode: function (image) {
        var html = '<div class="alert_red_qrcode StopPropagation"><p class="ercode"><img src="' + image + '" /></p><p>推荐朋友扫描以上二维码，关注并完善信息可获得金币奖励</p></div>';
        return html;
    },
    HTML_Prize: function (money, unit) {
        var html = '<div class="alert_tasknow StopPropagation">' +
                    '<p class="_close" onclick="_ALERT.Close();"><i class="fa fa-close"></i></p>' +
                    '<p class="prize">恭喜您中奖<span>' + money + '</span>' + (unit ? unit : '元') + '</p>' +
                    '<p class="desc clearfix">提示：中奖金额已注入你的红包，<a href="javascript:void(0);" onclick="_ALERT.Close();window.location.href=\'/views/dj/index#/redBagIndex\';">点击查看</a></p>' +
                    '<p class="submit"><a href="javascript:void(0);" onclick="_ALERT.Close();window.location.href=\'/dj/index#/index\';">去看广告赚更多的金币吧</a></p>' +
                    '<p class="desc clearfix">哇，你手开光了吧！<a href="javascript:void(0);" onclick="_ALERT.Close();window.location.href=\'/views/dj/index#/yaoyiyao_share?money=' + money + '&unit=' + unit + '\';">晒一晒朋友圈</a></p>' +
                    '<p class="desc clearfix mg_t10"><a href="javascript:void(0);" onclick="_ALERT.Close();window.location.href=\'/views/dj/index#/zhongjiangjilu?leibie=0\';" class="right">历史中奖记录</a></p>' +
                    '</div>';
        return html;
    },
    HTML_DrawApply: function (money) {
        var html = '<div class="alert_drawapply"><p>消费<span>' + money + '</span>金币</p><p><h1>报名成功 !</h1></p></div>';
        return html;
    }
};

$(function () {
    $(document).on(touch_end, "#ALERT_WIN", function (event) {
        event.stopPropagation();
        _ALERT.Close();
    });
    $(document).on(touch_end, ".StopPropagation", function (event) {
        event.stopPropagation();
    });

    $(document).keyup(function (event) {
        if (event.which == '27') {
            _ALERT.Close();
        }
    });
});



_systeme_time = null;
_system = {

    $: function (id) {
        return document.getElementById(id);
    },

    _client: function () {

        return {
            w: document.documentElement.scrollWidth,
            h: document.documentElement.scrollHeight,
            bw: document.documentElement.clientWidth,
            bh: document.documentElement.clientHeight
        };

    },

    _scroll: function () {

        return {
            x: document.documentElement.scrollLeft ? document.documentElement.scrollLeft
					: document.body.scrollLeft,
            y: document.documentElement.scrollTop ? document.documentElement.scrollTop
					: document.body.scrollTop
        };

    },

    _cover: function (show, num) {

        if (show) {

            if (!this.$("cover")) {
                var _html = '<div id="cover"></div><div id="guide"></div>';
                $("body").append(_html);
            }

            this.$("guide").innerHTML = '<img src="/dj/images/guide' + (!num ? 1 : num) + '.png">';

            this.$("cover").style.display = "block";

            this.$("cover").style.width = (this._client().bw > this._client().w ? this
					._client().bw
					: this._client().w)
					+ "px";

            this.$("cover").style.height = (this._client().bh > this._client().h ? this
					._client().bh
					: this._client().h)
					+ "px";

        } else {

            if (this.$("cover")) {
                this.$("cover").style.display = "none";
                this.$("guide").style.display = "none";
            }

        }

    },

    _guide: function (click, num) {

        this._cover(true, num);

        this.$("guide").style.display = "block";

        this.$("guide").style.top = (_system._scroll().y + 5) + "px";

        window.onresize = function () {
            _system._cover(true);
            _system.$("guide").style.top = (_system._scroll().y + 5) + "px";
        };

        if (click) {
            _system.$("cover").onclick = function () {

                _system._cover();

                _system.$("guide").style.display = "none";

                _system.$("cover").onclick = null;

                window.onresize = null;

            };
        }

        if (typeof (_systeme_time) != "undefined") {
            clearInterval(_systeme_time);
        }
        _systeme_time = setInterval(function () {
            _system._cover();
            _system.$("guide").style.display = "none";
            _system.$("cover").onclick = null;
            window.onresize = null;
            clearInterval(_systeme_time);
        }, 5000);

    },

    _zero: function (n) {

        return n < 0 ? 0 : n;

    }

}

var numberSpark = function (obj, startEl, endEl, dot) {
    var that = this;
    if (that.obj = obj, that.no = parseInt(that.obj.data("spark")), that.arr = [], isNaN(that.no)) {
        return !1;
    }
    that.frag = "",
    that.intv = 0,
    that.flag = 0,
    that.sn = "";
    for (var l = that.no.toString().length, i = l - 1; i >= 0; i--) {
        that.sn = that.no.toString().charAt(i),
        that.arr.push(that.sn);
    }
    that.spark = function () {
        if (that.frag = "", that.flag > 9) {
            return clearTimeout(that.intv),
            that.obj.attr("spark-done", !0),
            void 0;
        }
        for (var l = that.arr.length, i = l - 1; i >= 0; i--) {
            that.arr[i] = Number(that.arr[i]),
            that.arr[i] = that.arr[i] + 1 > 9 ? 0 : that.arr[i] + 1,
            that.frag += (startEl || "") + that.arr[i] + (endEl || ""),
            dot && 0 == i % 3 && 0 != i && (that.frag += dot);
        }
        that.obj.html(),
        that.obj.html(that.frag),
        that.flag++,
        that.intv = setTimeout(that.spark, 100);
    }
};